package so.ifl.view.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import so.ifl.commons.tools.StringHelper;
import so.ifl.commons.tools.SystemUtil;
import so.ifl.commons.tools.pinter.Iprinter;
import so.ifl.commons.tools.pinter.PrinterFactory;
import so.ifl.servcie.OnlineExamService;
import so.ifl.vo.OnlineExamVo;

/**
 *create by aaron.lu
 *create at 2013-9-15 下午4:01:00
 *<mailto: atyuan.lu@gmail.com>
 **/
public class DoExam extends HttpServlet {
	
	Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{
		
		String ex = (String)request.getParameter("ex");
		String n = (String)request.getParameter("n");
		Integer _n = 1;
		
		if (StringHelper.isNotEmpty(n)) {
			_n = Integer.parseInt(n);
		}
		
		ServletContext servletContext = request.getSession().getServletContext();		
		ApplicationContext appctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		
		OnlineExamService onlineExamService = (OnlineExamService)appctx.getBean("onlineExamService");
		String topics = "";
		RequestDispatcher rd = null;
		OnlineExamVo onlineExamVo = null;
		
		
		if (StringHelper.isNotEmpty(ex)) {
			if (ex.equals("1")) {
				topics = onlineExamService.getSubject1Imitate();
				request.setAttribute("total", "100");
				rd = getServletContext().getRequestDispatcher("/exam.jsp");
			}else if (ex.equals("4")) {
				topics = onlineExamService.getSubject4Imitate();
				request.setAttribute("total", "50");
				rd = getServletContext().getRequestDispatcher("/exam.jsp");
			}else if(ex.equals("4s")){
				
				
				onlineExamVo = onlineExamService.getOnlineExamVo(4, _n);
				topics = SystemUtil.onlineExamHTML(onlineExamVo);
				rd = getServletContext().getRequestDispatcher("/practise.jsp");
				request.setAttribute("onlineExamVo", onlineExamVo.toString());
			}else if(ex.equals("1s")){
				onlineExamVo = onlineExamService.getOnlineExamVo(1, _n);
				topics = SystemUtil.onlineExamHTML(onlineExamVo);
				rd = getServletContext().getRequestDispatcher("/practise.jsp");
				request.setAttribute("onlineExamVo", onlineExamVo.toString());
			}
			
		}		
		request.setAttribute("topics", topics);
		
		try {
			rd.forward(request, response);
		} catch (IOException e) {
			logger_.error(e.getMessage());
		}
		
	}
	
	
}
